import { defHttp } from '@/utils/http/axios';
import { ErrorMessageMode } from '#/axios';
import { BaseResponse, PagerResponse, AbsPageQuery } from '@/api/base';

// 定义接口地址
enum Api {
  /** 批量删除 */
  BATCH_DELETE = '/toolkit/table/batchDelete',
  /** 待导入表 */
  CANDIDATE_TABLES = '/toolkit/table/candidateTables',
  /** 删除 */
  DELETE_BY_ID = '/toolkit/table/delete/',
  /** 查询基本信息 */
  DETAIL = '/toolkit/table/',
  /** 批量导入数据表 */
  IMPORT_TABLES = '/toolkit/table/importTables',
  /** 列表查询 */
  LIST = '/toolkit/table/list',
  /** 分页查询 */
  PAGER = '/toolkit/table/pager',
  /** 新增 */
  SAVE = '/toolkit/table/save',
  /** 表结构同步 */
  SYNC = '/toolkit/table/sync/',
  /** 修改 */
  UPDATE = '/toolkit/table/update/',
}

// 定义参数类型

export interface MysqlTablePO {
  tableName?: string;
  tableSchema?: string;
  tableComment?: string;
  engine?: string;
  createTime?: Date;
}

export interface MysqlTableQuery extends AbsBaseQuery {
  tableSchema?: string;
  tableName?: string;
  tableComment?: string;
}

export interface TableDO {
  id?: number;
  creator?: number;
  createTime?: Date;
  updater?: number;
  updateTime?: Date;
  status?: number;
  schemaName?: string;
  tableName?: string;
  moduleName?: string;
  className?: string;
  comment?: string;
  hasPo?: number;
  hasMapper?: number;
  hasMapperXml?: number;
  hasDo?: number;
  hasServiceApi?: number;
  hasService?: number;
  hasRequest?: number;
  hasQuery?: number;
  hasController?: number;
  author?: string;
  hasApiTs?: number;
  hasIndexVue?: number;
  editComponent?: string;
  indexTemplate?: number;
  parentMenu?: number;
}

export interface TableQuery extends AbsPageQuery {
  id?: number;
  creator?: number;
  createTime?: Date;
  updater?: number;
  updateTime?: Date;
  status?: number;
  schemaName?: string;
  tableName?: string;
  moduleName?: string;
  className?: string;
  comment?: string;
  hasPo?: number;
  hasMapper?: number;
  hasMapperXml?: number;
  hasDo?: number;
  hasServiceApi?: number;
  hasService?: number;
  hasRequest?: number;
  hasQuery?: number;
  hasController?: number;
  author?: string;
  hasApiTs?: number;
  hasIndexVue?: number;
  editComponent?: string;
  indexTemplate?: number;
  parentMenu?: number;
}

export interface TableRequest {
  id?: number;
  creator?: number;
  createTime?: Date;
  updater?: number;
  updateTime?: Date;
  status?: number;
  schemaName?: string;
  tableName?: string;
  moduleName?: string;
  className?: string;
  comment?: string;
  hasPo?: number;
  hasMapper?: number;
  hasMapperXml?: number;
  hasDo?: number;
  hasServiceApi?: number;
  hasService?: number;
  hasRequest?: number;
  hasQuery?: number;
  hasController?: number;
  author?: string;
  hasApiTs?: number;
  hasIndexVue?: number;
  editComponent?: string;
  indexTemplate?: number;
  parentMenu?: number;
}

// 定义接口方法
// 批量删除
export function batchDelete(params: number[], mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<number>>(
    {
      url: Api.BATCH_DELETE,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// 待导入表
export function candidateTables(params: MysqlTableQuery, mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<Array<MysqlTablePO>>>(
    {
      url: Api.CANDIDATE_TABLES,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// 删除
export function deleteById(id: number, mode: ErrorMessageMode = 'message') {
  return defHttp.delete<BaseResponse<boolean>>(
    {
      url: Api.DELETE_BY_ID + '/' + id,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// 查询基本信息
export function detail(id: number | string, mode: ErrorMessageMode = 'message') {
  return defHttp.get<BaseResponse<TableDO>>(
    {
      url: Api.DETAIL + '/' + id,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// 批量导入数据表
export function importTables(params: string[], mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<boolean>>(
    {
      url: Api.IMPORT_TABLES,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// 列表查询
export function list(params: TableQuery, mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<Array<TableDO>>>(
    {
      url: Api.LIST,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// 分页查询
export function pager(params: TableQuery, mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<PagerResponse<TableDO>>>(
    {
      url: Api.PAGER,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// 新增
export function save(params: TableRequest, mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<number>>(
    {
      url: Api.SAVE,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// 表结构同步
export function sync(id: number, mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<number>>(
    {
      url: Api.SYNC + '/' + id,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// 修改
export function update(id: number, params: TableRequest, mode: ErrorMessageMode = 'message') {
  return defHttp.put<BaseResponse<boolean>>(
    {
      url: Api.UPDATE + '/' + id,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
